1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/dbus/easy_unlock/easy_unlock_client.h"
#include <stddef.h>
#include <stdint.h>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/components/dbus/easy_unlock/fake_easy_unlock_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace ash {
namespace {
EasyUnlockClient* g_instance = nullptr;
// Reads array of bytes from a dbus message reader and converts it to string.
std::string PopResponseData(dbus::MessageReader* reader) {
const uint8_t* bytes = NULL;
size_t length = 0;
if (!reader->PopArrayOfBytes(&bytes, &length))
return "";
return std::string(reinterpret_cast<const char*>(bytes), length);
}
// Converts string to array of bytes and writes it using dbus meddage writer.
void AppendStringAsByteArray(const std::string& data,
dbus::MessageWriter* writer) {
writer->AppendArrayOfBytes(base::as_byte_span(data));
}
// The EasyUnlockClient used in production.
class EasyUnlockClientImpl : public EasyUnlockClient {
public:
EasyUnlockClientImpl() : proxy_(nullptr) {}
EasyUnlockClientImpl(const EasyUnlockClientImpl&) = delete;
EasyUnlockClientImpl& operator=(const EasyUnlockClientImpl&) = delete;
~EasyUnlockClientImpl() override = default;
// EasyUnlockClient override.
void GenerateEcP256KeyPair(KeyPairCallback callback) override {
dbus::MethodCall method_call(easy_unlock::kEasyUnlockServiceInterface,
easy_unlock::kGenerateEcP256KeyPairMethod);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&EasyUnlockClientImpl::OnKeyPair,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
// EasyUnlockClient override.
void WrapPublicKey(const std::string& key_algorithm,
const std::string& public_key,
DataCallback callback) override {
dbus::MethodCall method_call(easy_unlock::kEasyUnlockServiceInterface,
easy_unlock::kWrapPublicKeyMethod);
dbus::MessageWriter writer(&method_call);
writer.AppendString(key_algorithm);
AppendStringAsByteArray(public_key, &writer);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&EasyUnlockClientImpl::OnData,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
// EasyUnlockClient override.
void PerformECDHKeyAgreement(const std::string& private_key,
const std::string& public_key,
DataCallback callback) override {
dbus::MethodCall method_call(easy_unlock::kEasyUnlockServiceInterface,
easy_unlock::kPerformECDHKeyAgreementMethod);
dbus::MessageWriter writer(&method_call);
// NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
// not guaranteed here, so the method uses byte arrays.
AppendStringAsByteArray(private_key, &writer);
AppendStringAsByteArray(public_key, &writer);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&EasyUnlockClientImpl::OnData,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
// EasyUnlockClient override.
void CreateSecureMessage(const std::string& payload,
const CreateSecureMessageOptions& options,
DataCallback callback) override {
dbus::MethodCall method_call(easy_unlock::kEasyUnlockServiceInterface,
easy_unlock::kCreateSecureMessageMethod);
dbus::MessageWriter writer(&method_call);
// NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
// not guaranteed here, so the method uses byte arrays.
AppendStringAsByteArray(payload, &writer);
AppendStringAsByteArray(options.key, &writer);
AppendStringAsByteArray(options.associated_data, &writer);
AppendStringAsByteArray(options.public_metadata, &writer);
AppendStringAsByteArray(options.verification_key_id, &writer);
AppendStringAsByteArray(options.decryption_key_id, &writer);
writer.AppendString(options.encryption_type);
writer.AppendString(options.signature_type);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&EasyUnlockClientImpl::OnData,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
// EasyUnlockClient override.
void UnwrapSecureMessage(const std::string& message,
const UnwrapSecureMessageOptions& options,
DataCallback callback) override {
dbus::MethodCall method_call(easy_unlock::kEasyUnlockServiceInterface,
easy_unlock::kUnwrapSecureMessageMethod);
dbus::MessageWriter writer(&method_call);
// NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
// not guaranteed here, so the method uses byte arrays.
AppendStringAsByteArray(message, &writer);
AppendStringAsByteArray(options.key, &writer);
AppendStringAsByteArray(options.associated_data, &writer);
writer.AppendString(options.encryption_type);
writer.AppendString(options.signature_type);
proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&EasyUnlockClientImpl::OnData,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void Init(dbus::Bus* bus) override {
proxy_ = bus->GetObjectProxy(
easy_unlock::kEasyUnlockServiceName,
dbus::ObjectPath(easy_unlock::kEasyUnlockServicePath));
}
private:
void OnData(DataCallback callback, dbus::Response* response) {
if (!response) {
std::move(callback).Run(std::string());
return;
}
dbus::MessageReader reader(response);
std::move(callback).Run(PopResponseData(&reader));
}
void OnKeyPair(KeyPairCallback callback, dbus::Response* response) {
if (!response) {
std::move(callback).Run(std::string(), std::string());
return;
}
dbus::MessageReader reader(response);
std::string private_key = PopResponseData(&reader);
std::string public_key = PopResponseData(&reader);
if (public_key.empty() || private_key.empty()) {
std::move(callback).Run(std::string(), std::string());
return;
}
std::move(callback).Run(private_key, public_key);
}
raw_ptr<dbus::ObjectProxy> proxy_;
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<EasyUnlockClientImpl> weak_ptr_factory_{this};
};
} // namespace
EasyUnlockClient::CreateSecureMessageOptions::CreateSecureMessageOptions() =
default;
EasyUnlockClient::CreateSecureMessageOptions::~CreateSecureMessageOptions() =
default;
EasyUnlockClient::UnwrapSecureMessageOptions::UnwrapSecureMessageOptions() =
default;
EasyUnlockClient::UnwrapSecureMessageOptions::~UnwrapSecureMessageOptions() =
default;
// static
EasyUnlockClient* EasyUnlockClient::Get() {
return g_instance;
}
// static
void EasyUnlockClient::Initialize(dbus::Bus* bus) {
CHECK(bus);
(new EasyUnlockClientImpl())->Init(bus);
}
// static
void EasyUnlockClient::InitializeFake() {
(new FakeEasyUnlockClient())->Init(nullptr);
}
// static
void EasyUnlockClient::Shutdown() {
CHECK(g_instance);
delete g_instance;
}
EasyUnlockClient::EasyUnlockClient() {
CHECK(!g_instance);
g_instance = this;
}
EasyUnlockClient::~EasyUnlockClient() {
CHECK_EQ(g_instance, this);
g_instance = nullptr;
}
} // namespace ash
|