Commit be4381d4 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Migrate DataMethodCallback into DBusMethodCallback.

BUG=739622
TEST=Build.

Change-Id: I196aea8c5d66c020a22c6eceb899783da3b2cd3a
Reviewed-on: https://chromium-review.googlesource.com/697047
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506829}
parent 1bb3d17c
...@@ -76,17 +76,15 @@ void DBusStringCallback( ...@@ -76,17 +76,15 @@ void DBusStringCallback(
const base::Callback<void(const std::string&)> on_success, const base::Callback<void(const std::string&)> on_success,
const base::Closure& on_failure, const base::Closure& on_failure,
const base::Location& from_here, const base::Location& from_here,
chromeos::DBusMethodCallStatus status, base::Optional<chromeos::CryptohomeClient::TpmAttestationDataResult>
bool result, result) {
const std::string& data) { if (!result.has_value() || !result->success) {
if (status != chromeos::DBUS_METHOD_CALL_SUCCESS || !result) { LOG(ERROR) << "Cryptohome DBus method failed: " << from_here.ToString();
LOG(ERROR) << "Cryptohome DBus method failed: " << from_here.ToString()
<< " - " << status << " - " << result;
if (!on_failure.is_null()) if (!on_failure.is_null())
on_failure.Run(); on_failure.Run();
return; return;
} }
on_success.Run(data); on_success.Run(result->data);
} }
} // namespace } // namespace
...@@ -189,12 +187,19 @@ void AttestationPolicyObserver::GetNewCertificate() { ...@@ -189,12 +187,19 @@ void AttestationPolicyObserver::GetNewCertificate() {
EmptyAccountId(), // Not used. EmptyAccountId(), // Not used.
std::string(), // Not used. std::string(), // Not used.
true, // Force a new key to be generated. true, // Force a new key to be generated.
base::Bind(DBusStringCallback, base::Bind(
base::Bind(&AttestationPolicyObserver::UploadCertificate, [](const base::Callback<void(const std::string&)> on_success,
weak_factory_.GetWeakPtr()), const base::Closure& on_failure, const base::Location& from_here,
base::Bind(&AttestationPolicyObserver::Reschedule, bool success, const std::string& data) {
weak_factory_.GetWeakPtr()), DBusStringCallback(on_success, on_failure, from_here,
FROM_HERE, DBUS_METHOD_CALL_SUCCESS)); CryptohomeClient::TpmAttestationDataResult{
success, std::move(data)});
},
base::Bind(&AttestationPolicyObserver::UploadCertificate,
weak_factory_.GetWeakPtr()),
base::Bind(&AttestationPolicyObserver::Reschedule,
weak_factory_.GetWeakPtr()),
FROM_HERE));
} }
void AttestationPolicyObserver::GetExistingCertificate() { void AttestationPolicyObserver::GetExistingCertificate() {
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/optional.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
#include "chromeos/cryptohome/async_method_caller.h" #include "chromeos/cryptohome/async_method_caller.h"
#include "chromeos/cryptohome/cryptohome_parameters.h" #include "chromeos/cryptohome/cryptohome_parameters.h"
...@@ -55,17 +56,15 @@ void DBusBoolRedirectCallback(const base::Closure& on_true, ...@@ -55,17 +56,15 @@ void DBusBoolRedirectCallback(const base::Closure& on_true,
void DBusDataMethodCallback( void DBusDataMethodCallback(
const AttestationFlow::CertificateCallback& callback, const AttestationFlow::CertificateCallback& callback,
DBusMethodCallStatus status, base::Optional<CryptohomeClient::TpmAttestationDataResult> result) {
bool result, if (!result.has_value()) {
const std::string& data) {
if (status != DBUS_METHOD_CALL_SUCCESS) {
LOG(ERROR) << "Attestation: DBus data operation failed."; LOG(ERROR) << "Attestation: DBus data operation failed.";
if (!callback.is_null()) if (!callback.is_null())
callback.Run(false, ""); callback.Run(false, "");
return; return;
} }
if (!callback.is_null()) if (!callback.is_null())
callback.Run(result, data); callback.Run(result->success, result->data);
} }
} // namespace } // namespace
......
...@@ -587,7 +587,7 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -587,7 +587,7 @@ class CryptohomeClientImpl : public CryptohomeClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override { DBusMethodCallback<TpmAttestationDataResult> callback) override {
dbus::MethodCall method_call( dbus::MethodCall method_call(
cryptohome::kCryptohomeInterface, cryptohome::kCryptohomeInterface,
cryptohome::kCryptohomeTpmAttestationGetCertificate); cryptohome::kCryptohomeTpmAttestationGetCertificate);
...@@ -598,8 +598,8 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -598,8 +598,8 @@ class CryptohomeClientImpl : public CryptohomeClient {
writer.AppendString(key_name); writer.AppendString(key_name);
proxy_->CallMethod( proxy_->CallMethod(
&method_call, kTpmDBusTimeoutMs, &method_call, kTpmDBusTimeoutMs,
base::BindOnce(&CryptohomeClientImpl::OnDataMethod, base::BindOnce(&CryptohomeClientImpl::OnTpmAttestationDataMethod,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
// CryptohomeClient override. // CryptohomeClient override.
...@@ -607,7 +607,7 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -607,7 +607,7 @@ class CryptohomeClientImpl : public CryptohomeClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override { DBusMethodCallback<TpmAttestationDataResult> callback) override {
dbus::MethodCall method_call( dbus::MethodCall method_call(
cryptohome::kCryptohomeInterface, cryptohome::kCryptohomeInterface,
cryptohome::kCryptohomeTpmAttestationGetPublicKey); cryptohome::kCryptohomeTpmAttestationGetPublicKey);
...@@ -618,8 +618,8 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -618,8 +618,8 @@ class CryptohomeClientImpl : public CryptohomeClient {
writer.AppendString(key_name); writer.AppendString(key_name);
proxy_->CallMethod( proxy_->CallMethod(
&method_call, kTpmDBusTimeoutMs, &method_call, kTpmDBusTimeoutMs,
base::BindOnce(&CryptohomeClientImpl::OnDataMethod, base::BindOnce(&CryptohomeClientImpl::OnTpmAttestationDataMethod,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
// CryptohomeClient override. // CryptohomeClient override.
...@@ -702,7 +702,7 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -702,7 +702,7 @@ class CryptohomeClientImpl : public CryptohomeClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override { DBusMethodCallback<TpmAttestationDataResult> callback) override {
dbus::MethodCall method_call( dbus::MethodCall method_call(
cryptohome::kCryptohomeInterface, cryptohome::kCryptohomeInterface,
cryptohome::kCryptohomeTpmAttestationGetKeyPayload); cryptohome::kCryptohomeTpmAttestationGetKeyPayload);
...@@ -713,8 +713,8 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -713,8 +713,8 @@ class CryptohomeClientImpl : public CryptohomeClient {
writer.AppendString(key_name); writer.AppendString(key_name);
proxy_->CallMethod( proxy_->CallMethod(
&method_call, kTpmDBusTimeoutMs, &method_call, kTpmDBusTimeoutMs,
base::BindOnce(&CryptohomeClientImpl::OnDataMethod, base::BindOnce(&CryptohomeClientImpl::OnTpmAttestationDataMethod,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
// CryptohomeClient override. // CryptohomeClient override.
...@@ -1112,23 +1112,24 @@ class CryptohomeClientImpl : public CryptohomeClient { ...@@ -1112,23 +1112,24 @@ class CryptohomeClientImpl : public CryptohomeClient {
} }
// Handles responses for methods with a bool result and data. // Handles responses for methods with a bool result and data.
void OnDataMethod(const DataMethodCallback& callback, void OnTpmAttestationDataMethod(
dbus::Response* response) { DBusMethodCallback<TpmAttestationDataResult> callback,
dbus::Response* response) {
if (!response) { if (!response) {
callback.Run(DBUS_METHOD_CALL_FAILURE, false, std::string()); std::move(callback).Run(base::nullopt);
return; return;
} }
dbus::MessageReader reader(response); dbus::MessageReader reader(response);
const uint8_t* data_buffer = NULL; TpmAttestationDataResult result;
const uint8_t* data_buffer = nullptr;
size_t data_length = 0; size_t data_length = 0;
bool result = false;
if (!reader.PopArrayOfBytes(&data_buffer, &data_length) || if (!reader.PopArrayOfBytes(&data_buffer, &data_length) ||
!reader.PopBool(&result)) { !reader.PopBool(&result.success)) {
callback.Run(DBUS_METHOD_CALL_FAILURE, false, std::string()); std::move(callback).Run(base::nullopt);
return; return;
} }
std::string data(reinterpret_cast<const char*>(data_buffer), data_length); result.data.assign(reinterpret_cast<const char*>(data_buffer), data_length);
callback.Run(DBUS_METHOD_CALL_SUCCESS, result, data); std::move(callback).Run(std::move(result));
} }
// Handles responses for methods with a BaseReply protobuf method. // Handles responses for methods with a BaseReply protobuf method.
......
...@@ -65,10 +65,6 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient { ...@@ -65,10 +65,6 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
// A callback to handle LowDiskSpace signals. // A callback to handle LowDiskSpace signals.
typedef base::Callback<void(uint64_t disk_free_bytes)> LowDiskSpaceHandler; typedef base::Callback<void(uint64_t disk_free_bytes)> LowDiskSpaceHandler;
// A callback for methods which return both a bool result and data.
typedef base::Callback<void(DBusMethodCallStatus call_status,
bool result,
const std::string& data)> DataMethodCallback;
// A callback to handle DircryptoMigrationProgress signals. // A callback to handle DircryptoMigrationProgress signals.
typedef base::Callback<void(cryptohome::DircryptoMigrationStatus status, typedef base::Callback<void(cryptohome::DircryptoMigrationStatus status,
...@@ -76,6 +72,15 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient { ...@@ -76,6 +72,15 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
uint64_t total)> uint64_t total)>
DircryptoMigrationProgessHandler; DircryptoMigrationProgessHandler;
// Represents the result to obtain the data related to TPM attestation.
struct TpmAttestationDataResult {
// True when it is succeeded to obtain the data.
bool success = false;
// The returned content. Available iff |success| is true.
std::string data;
};
// TPM Token Information retrieved from cryptohome. // TPM Token Information retrieved from cryptohome.
// For invalid token |label| and |user_pin| will be empty, while |slot| will // For invalid token |label| and |user_pin| will be empty, while |slot| will
// be set to -1. // be set to -1.
...@@ -394,7 +399,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient { ...@@ -394,7 +399,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) = 0; DBusMethodCallback<TpmAttestationDataResult> callback) = 0;
// Gets the public key for the key specified by |key_type| and |key_name|. // Gets the public key for the key specified by |key_type| and |key_name|.
// |callback| will be called when the operation completes. If the key does // |callback| will be called when the operation completes. If the key does
...@@ -405,7 +410,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient { ...@@ -405,7 +410,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) = 0; DBusMethodCallback<TpmAttestationDataResult> callback) = 0;
// Asynchronously registers an attestation key with the current user's // Asynchronously registers an attestation key with the current user's
// PKCS #11 token. The |callback| will be called when the dbus call // PKCS #11 token. The |callback| will be called when the dbus call
...@@ -463,7 +468,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient { ...@@ -463,7 +468,7 @@ class CHROMEOS_EXPORT CryptohomeClient : public DBusClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) = 0; DBusMethodCallback<TpmAttestationDataResult> callback) = 0;
// Sets the |payload| associated with the key specified by |key_type| and // Sets the |payload| associated with the key specified by |key_type| and
// |key_name|. The |callback| will be called when the operation completes. // |key_name|. The |callback| will be called when the operation completes.
......
...@@ -450,41 +450,39 @@ void FakeCryptohomeClient::TpmAttestationGetCertificate( ...@@ -450,41 +450,39 @@ void FakeCryptohomeClient::TpmAttestationGetCertificate(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) { DBusMethodCallback<TpmAttestationDataResult> callback) {
bool result = false; TpmAttestationDataResult result;
std::string certificate;
switch (key_type) { switch (key_type) {
case attestation::KEY_DEVICE: { case attestation::KEY_DEVICE: {
const auto it = device_certificate_map_.find(key_name); const auto it = device_certificate_map_.find(key_name);
if (it != device_certificate_map_.end()) { if (it != device_certificate_map_.end()) {
result = true; result.success = true;
certificate = it->second; result.data = it->second;
} }
break; break;
} }
case attestation::KEY_USER: { case attestation::KEY_USER: {
const auto it = user_certificate_map_.find({cryptohome_id, key_name}); const auto it = user_certificate_map_.find({cryptohome_id, key_name});
if (it != user_certificate_map_.end()) { if (it != user_certificate_map_.end()) {
result = true; result.success = true;
certificate = it->second; result.data = it->second;
} }
break; break;
} }
} }
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE, base::BindOnce(std::move(callback), std::move(result)));
base::BindOnce(callback, DBUS_METHOD_CALL_SUCCESS, result, certificate));
} }
void FakeCryptohomeClient::TpmAttestationGetPublicKey( void FakeCryptohomeClient::TpmAttestationGetPublicKey(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) { DBusMethodCallback<TpmAttestationDataResult> callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, false, std::string())); base::BindOnce(std::move(callback), TpmAttestationDataResult{}));
} }
void FakeCryptohomeClient::TpmAttestationRegisterKey( void FakeCryptohomeClient::TpmAttestationRegisterKey(
...@@ -523,20 +521,18 @@ void FakeCryptohomeClient::TpmAttestationGetKeyPayload( ...@@ -523,20 +521,18 @@ void FakeCryptohomeClient::TpmAttestationGetKeyPayload(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) { DBusMethodCallback<TpmAttestationDataResult> callback) {
bool result = false; TpmAttestationDataResult result;
std::string payload;
if (key_type == attestation::KEY_DEVICE) { if (key_type == attestation::KEY_DEVICE) {
const auto it = device_key_payload_map_.find(key_name); const auto it = device_key_payload_map_.find(key_name);
if (it != device_key_payload_map_.end()) { if (it != device_key_payload_map_.end()) {
result = true; result.success = true;
payload = it->second; result.data = it->second;
} }
} }
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, FROM_HERE, base::BindOnce(std::move(callback), std::move(result)));
base::BindOnce(callback, DBUS_METHOD_CALL_SUCCESS, result, payload));
} }
void FakeCryptohomeClient::TpmAttestationSetKeyPayload( void FakeCryptohomeClient::TpmAttestationSetKeyPayload(
......
...@@ -127,12 +127,12 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient { ...@@ -127,12 +127,12 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override; DBusMethodCallback<TpmAttestationDataResult> callback) override;
void TpmAttestationGetPublicKey( void TpmAttestationGetPublicKey(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override; DBusMethodCallback<TpmAttestationDataResult> callback) override;
void TpmAttestationRegisterKey( void TpmAttestationRegisterKey(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
...@@ -157,7 +157,7 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient { ...@@ -157,7 +157,7 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
const std::string& key_name, const std::string& key_name,
const DataMethodCallback& callback) override; DBusMethodCallback<TpmAttestationDataResult> callback) override;
void TpmAttestationSetKeyPayload( void TpmAttestationSetKeyPayload(
attestation::AttestationKeyType key_type, attestation::AttestationKeyType key_type,
const cryptohome::Identification& cryptohome_id, const cryptohome::Identification& cryptohome_id,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment