Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: chrome/browser/chromeos/attestation/attestation_policy_observer.cc

Issue 12556004: Created AttestationPolicyObserver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/browser_thread.h"
21 #include "content/public/browser/notification_details.h"
22
23 namespace {
24
25 // A dbus callback which handles a boolean result.
26 //
27 // Parameters
28 // on_true - Called when status=success and value=true.
29 // on_false - Called when status=success and value=false.
30 // status - The dbus operation status.
31 // value - The value returned by the dbus operation.
32 void DBusBoolRedirectCallback(const base::Closure& on_true,
33 const base::Closure& on_false,
34 chromeos::DBusMethodCallStatus status,
35 bool value) {
36 if (status != chromeos::DBUS_METHOD_CALL_SUCCESS)
37 return;
38 const base::Closure& task = value ? on_true : on_false;
39 if (!task.is_null())
40 task.Run();
41 }
42
43 // A dbus callback which handles a string result.
44 //
45 // Parameters
46 // on_success - Called when status=success and result=true.
47 // status - The dbus operation status.
48 // result - The result returned by the dbus operation.
49 // data - The data returned by the dbus operation.
50 void DBusStringCallback(
51 const base::Callback<void(const std::string&)> on_success,
52 chromeos::DBusMethodCallStatus status,
53 bool result,
54 const std::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 : cros_settings_(CrosSettings::Get()),
71 policy_client_(policy_client),
72 cryptohome_client_(NULL),
73 attestation_flow_(NULL),
74 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
75 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
76 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this);
77 Start();
78 }
79
80 AttestationPolicyObserver::AttestationPolicyObserver(
81 policy::CloudPolicyClient* policy_client,
82 CryptohomeClient* cryptohome_client,
83 AttestationFlow* attestation_flow)
84 : cros_settings_(CrosSettings::Get()),
85 policy_client_(policy_client),
86 cryptohome_client_(cryptohome_client),
87 attestation_flow_(attestation_flow),
88 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
90 cros_settings_->AddSettingsObserver(kDeviceAttestationEnabled, this);
91 Start();
92 }
93
94 AttestationPolicyObserver::~AttestationPolicyObserver() {
95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
96 cros_settings_->RemoveSettingsObserver(kDeviceAttestationEnabled, this);
97 }
98
99 void AttestationPolicyObserver::Observe(
100 int type,
101 const content::NotificationSource& source,
102 const content::NotificationDetails& details) {
103 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
104 std::string* path = content::Details<std::string>(details).ptr();
105 if (type != chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED ||
106 *path != kDeviceAttestationEnabled) {
107 LOG(WARNING) << "AttestationPolicyObserver: Unexpected event received.";
108 return;
109 }
110 Start();
111 }
112
113 void AttestationPolicyObserver::Start() {
114 // If attestation is not enabled, there is nothing to do.
115 bool enabled = false;
116 if (!cros_settings_->GetBoolean(kDeviceAttestationEnabled, &enabled) ||
117 !enabled)
118 return;
119
120 // We expect a registered CloudPolicyClient.
121 if (!policy_client_->is_registered()) {
122 LOG(ERROR) << "AttestationPolicyObserver: Invalid CloudPolicyClient.";
123 return;
124 }
125
126 if (!cryptohome_client_)
127 cryptohome_client_ = DBusThreadManager::Get()->GetCryptohomeClient();
128
129 if (!attestation_flow_) {
130 scoped_ptr<ServerProxy> attestation_ca_client(new AttestationCAClient());
131 default_attestation_flow_.reset(new AttestationFlow(
132 cryptohome::AsyncMethodCaller::GetInstance(),
133 cryptohome_client_,
134 attestation_ca_client.Pass()));
135 attestation_flow_ = default_attestation_flow_.get();
136 }
137
138 // Start a dbus call to check if an Enterprise Machine Key already exists.
139 base::Closure on_does_exist =
140 base::Bind(&AttestationPolicyObserver::GetExistingCertificate,
141 weak_factory_.GetWeakPtr());
142 base::Closure on_does_not_exist =
143 base::Bind(&AttestationPolicyObserver::GetNewCertificate,
144 weak_factory_.GetWeakPtr());
145 cryptohome_client_->TpmAttestationDoesKeyExist(
146 CryptohomeClient::DEVICE_KEY,
147 kEnterpriseMachineKey,
148 base::Bind(DBusBoolRedirectCallback, on_does_exist, on_does_not_exist));
149 }
150
151 void AttestationPolicyObserver::GetNewCertificate() {
152 // We can reuse the dbus callback handler logic.
153 attestation_flow_->GetCertificate(
154 kEnterpriseMachineKey,
155 base::Bind(DBusStringCallback,
156 base::Bind(&AttestationPolicyObserver::UploadCertificate,
157 weak_factory_.GetWeakPtr()),
158 DBUS_METHOD_CALL_SUCCESS));
159 }
160
161 void AttestationPolicyObserver::GetExistingCertificate() {
162 cryptohome_client_->TpmAttestationGetCertificate(
163 CryptohomeClient::DEVICE_KEY,
164 kEnterpriseMachineKey,
165 base::Bind(DBusStringCallback,
166 base::Bind(&AttestationPolicyObserver::CheckCertificateExpiry,
167 weak_factory_.GetWeakPtr())));
168 }
169
170 void AttestationPolicyObserver::CheckCertificateExpiry(
171 const std::string& certificate) {
172 // TODO(dkrahn): Check if the certificate will expire soon, for now assume no.
173 CheckIfUploaded(certificate);
174 }
175
176 void AttestationPolicyObserver::UploadCertificate(
177 const std::string& certificate) {
178 // TODO(dkrahn): Upload the certificate.
179 }
180
181 void AttestationPolicyObserver::CheckIfUploaded(
182 const std::string& certificate) {
183 // TODO(dkrahn): Check if we've already uploaded the certificate.
184 }
185
186 } // namespace attestation
187 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698