Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chromeos / dbus / easy_unlock_client.cc
blob60040c018c95e5eeb5d77d21fc7f7ecfd2e69585
1 // Copyright 2014 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.
5 #include "chromeos/dbus/easy_unlock_client.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "dbus/bus.h"
12 #include "dbus/message.h"
13 #include "dbus/object_path.h"
14 #include "dbus/object_proxy.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 namespace chromeos {
19 namespace {
21 // Reads array of bytes from a dbus message reader and converts it to string.
22 std::string PopResponseData(dbus::MessageReader* reader) {
23 const uint8* bytes = NULL;
24 size_t length = 0;
25 if (!reader->PopArrayOfBytes(&bytes, &length))
26 return "";
28 return std::string(reinterpret_cast<const char*>(bytes), length);
31 // Converts string to array of bytes and writes it using dbus meddage writer.
32 void AppendStringAsByteArray(const std::string& data,
33 dbus::MessageWriter* writer) {
34 writer->AppendArrayOfBytes(reinterpret_cast<const uint8*>(data.data()),
35 data.length());
38 // The EasyUnlockClient used in production (and returned by
39 // EasyUnlockClient::Create).
40 class EasyUnlockClientImpl : public EasyUnlockClient {
41 public:
42 EasyUnlockClientImpl() : proxy_(NULL), weak_ptr_factory_(this) {}
44 virtual ~EasyUnlockClientImpl() {}
46 // EasyUnlockClient override.
47 virtual void PerformECDHKeyAgreement(const std::string& private_key,
48 const std::string& public_key,
49 const DataCallback& callback) OVERRIDE {
50 dbus::MethodCall method_call(
51 easy_unlock::kEasyUnlockServiceInterface,
52 easy_unlock::kPerformECDHKeyAgreementMethod);
53 dbus::MessageWriter writer(&method_call);
54 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
55 // not guaranteed here, so the method uses byte arrays.
56 AppendStringAsByteArray(private_key, &writer);
57 AppendStringAsByteArray(public_key, &writer);
58 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
59 base::Bind(&EasyUnlockClientImpl::OnData,
60 weak_ptr_factory_.GetWeakPtr(),
61 callback));
64 // EasyUnlockClient override.
65 virtual void GenerateEcP256KeyPair(const KeyPairCallback& callback) OVERRIDE {
66 dbus::MethodCall method_call(
67 easy_unlock::kEasyUnlockServiceInterface,
68 easy_unlock::kGenerateEcP256KeyPairMethod);
69 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
70 base::Bind(&EasyUnlockClientImpl::OnKeyPair,
71 weak_ptr_factory_.GetWeakPtr(),
72 callback));
75 // EasyUnlockClient override.
76 virtual void CreateSecureMessage(const std::string& payload,
77 const std::string& secret_key,
78 const std::string& associated_data,
79 const std::string& public_metadata,
80 const std::string& verification_key_id,
81 const std::string& encryption_type,
82 const std::string& signature_type,
83 const DataCallback& callback) OVERRIDE {
84 dbus::MethodCall method_call(
85 easy_unlock::kEasyUnlockServiceInterface,
86 easy_unlock::kCreateSecureMessageMethod);
87 dbus::MessageWriter writer(&method_call);
88 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
89 // not guaranteed here, so the method uses byte arrays.
90 AppendStringAsByteArray(payload, &writer);
91 AppendStringAsByteArray(secret_key, &writer);
92 AppendStringAsByteArray(associated_data, &writer);
93 AppendStringAsByteArray(public_metadata, &writer);
94 AppendStringAsByteArray(verification_key_id, &writer);
95 writer.AppendString(encryption_type);
96 writer.AppendString(signature_type);
97 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
98 base::Bind(&EasyUnlockClientImpl::OnData,
99 weak_ptr_factory_.GetWeakPtr(),
100 callback));
103 // EasyUnlockClient override.
104 virtual void UnwrapSecureMessage(const std::string& message,
105 const std::string& secret_key,
106 const std::string& associated_data,
107 const std::string& encryption_type,
108 const std::string& signature_type,
109 const DataCallback& callback) OVERRIDE {
110 dbus::MethodCall method_call(
111 easy_unlock::kEasyUnlockServiceInterface,
112 easy_unlock::kUnwrapSecureMessageMethod);
113 dbus::MessageWriter writer(&method_call);
114 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
115 // not guaranteed here, so the method uses byte arrays.
116 AppendStringAsByteArray(message, &writer);
117 AppendStringAsByteArray(secret_key, &writer);
118 AppendStringAsByteArray(associated_data, &writer);
119 writer.AppendString(encryption_type);
120 writer.AppendString(signature_type);
121 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
122 base::Bind(&EasyUnlockClientImpl::OnData,
123 weak_ptr_factory_.GetWeakPtr(),
124 callback));
127 protected:
128 virtual void Init(dbus::Bus* bus) OVERRIDE {
129 proxy_ =
130 bus->GetObjectProxy(
131 easy_unlock::kEasyUnlockServiceName,
132 dbus::ObjectPath(easy_unlock::kEasyUnlockServicePath));
135 private:
136 void OnData(const DataCallback& callback, dbus::Response* response) {
137 if (!response) {
138 callback.Run("");
139 return;
142 dbus::MessageReader reader(response);
143 callback.Run(PopResponseData(&reader));
146 void OnKeyPair(const KeyPairCallback& callback, dbus::Response* response) {
147 if (!response) {
148 callback.Run("", "");
149 return;
152 dbus::MessageReader reader(response);
153 std::string private_key = PopResponseData(&reader);
154 std::string public_key = PopResponseData(&reader);
156 if (public_key.empty() || private_key.empty()) {
157 callback.Run("", "");
158 return;
161 callback.Run(private_key, public_key);
164 dbus::ObjectProxy* proxy_;
166 // Note: This should remain the last member so it'll be destroyed and
167 // invalidate its weak pointers before any other members are destroyed.
168 base::WeakPtrFactory<EasyUnlockClientImpl> weak_ptr_factory_;
170 DISALLOW_COPY_AND_ASSIGN(EasyUnlockClientImpl);
173 } // namespace
175 EasyUnlockClient::EasyUnlockClient() {
178 EasyUnlockClient::~EasyUnlockClient() {
181 // static
182 EasyUnlockClient* EasyUnlockClient::Create() {
183 return new EasyUnlockClientImpl();
186 } // namespace chromeos