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"
10 #include "base/compiler_specific.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"
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
;
25 if (!reader
->PopArrayOfBytes(&bytes
, &length
))
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()),
38 // The EasyUnlockClient used in production (and returned by
39 // EasyUnlockClient::Create).
40 class EasyUnlockClientImpl
: public EasyUnlockClient
{
42 EasyUnlockClientImpl() : proxy_(NULL
), weak_ptr_factory_(this) {}
44 ~EasyUnlockClientImpl() override
{}
46 // EasyUnlockClient override.
47 void GenerateEcP256KeyPair(const KeyPairCallback
& callback
) override
{
48 dbus::MethodCall
method_call(
49 easy_unlock::kEasyUnlockServiceInterface
,
50 easy_unlock::kGenerateEcP256KeyPairMethod
);
51 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
52 base::Bind(&EasyUnlockClientImpl::OnKeyPair
,
53 weak_ptr_factory_
.GetWeakPtr(),
57 // EasyUnlockClient override.
58 void WrapPublicKey(const std::string
& key_algorithm
,
59 const std::string
& public_key
,
60 const DataCallback
& callback
) override
{
61 dbus::MethodCall
method_call(
62 easy_unlock::kEasyUnlockServiceInterface
,
63 easy_unlock::kWrapPublicKeyMethod
);
64 dbus::MessageWriter
writer(&method_call
);
65 writer
.AppendString(key_algorithm
);
66 AppendStringAsByteArray(public_key
, &writer
);
67 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
68 base::Bind(&EasyUnlockClientImpl::OnData
,
69 weak_ptr_factory_
.GetWeakPtr(),
73 // EasyUnlockClient override.
74 void PerformECDHKeyAgreement(const std::string
& private_key
,
75 const std::string
& public_key
,
76 const DataCallback
& callback
) override
{
77 dbus::MethodCall
method_call(
78 easy_unlock::kEasyUnlockServiceInterface
,
79 easy_unlock::kPerformECDHKeyAgreementMethod
);
80 dbus::MessageWriter
writer(&method_call
);
81 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
82 // not guaranteed here, so the method uses byte arrays.
83 AppendStringAsByteArray(private_key
, &writer
);
84 AppendStringAsByteArray(public_key
, &writer
);
85 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
86 base::Bind(&EasyUnlockClientImpl::OnData
,
87 weak_ptr_factory_
.GetWeakPtr(),
91 // EasyUnlockClient override.
92 void CreateSecureMessage(const std::string
& payload
,
93 const CreateSecureMessageOptions
& options
,
94 const DataCallback
& callback
) override
{
95 dbus::MethodCall
method_call(
96 easy_unlock::kEasyUnlockServiceInterface
,
97 easy_unlock::kCreateSecureMessageMethod
);
98 dbus::MessageWriter
writer(&method_call
);
99 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
100 // not guaranteed here, so the method uses byte arrays.
101 AppendStringAsByteArray(payload
, &writer
);
102 AppendStringAsByteArray(options
.key
, &writer
);
103 AppendStringAsByteArray(options
.associated_data
, &writer
);
104 AppendStringAsByteArray(options
.public_metadata
, &writer
);
105 AppendStringAsByteArray(options
.verification_key_id
, &writer
);
106 AppendStringAsByteArray(options
.decryption_key_id
, &writer
);
107 writer
.AppendString(options
.encryption_type
);
108 writer
.AppendString(options
.signature_type
);
109 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
110 base::Bind(&EasyUnlockClientImpl::OnData
,
111 weak_ptr_factory_
.GetWeakPtr(),
115 // EasyUnlockClient override.
116 void UnwrapSecureMessage(const std::string
& message
,
117 const UnwrapSecureMessageOptions
& options
,
118 const DataCallback
& callback
) override
{
119 dbus::MethodCall
method_call(
120 easy_unlock::kEasyUnlockServiceInterface
,
121 easy_unlock::kUnwrapSecureMessageMethod
);
122 dbus::MessageWriter
writer(&method_call
);
123 // NOTE: DBus expects that data sent as string is UTF-8 encoded. This is
124 // not guaranteed here, so the method uses byte arrays.
125 AppendStringAsByteArray(message
, &writer
);
126 AppendStringAsByteArray(options
.key
, &writer
);
127 AppendStringAsByteArray(options
.associated_data
, &writer
);
128 writer
.AppendString(options
.encryption_type
);
129 writer
.AppendString(options
.signature_type
);
130 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
131 base::Bind(&EasyUnlockClientImpl::OnData
,
132 weak_ptr_factory_
.GetWeakPtr(),
137 void Init(dbus::Bus
* bus
) override
{
140 easy_unlock::kEasyUnlockServiceName
,
141 dbus::ObjectPath(easy_unlock::kEasyUnlockServicePath
));
145 void OnData(const DataCallback
& callback
, dbus::Response
* response
) {
151 dbus::MessageReader
reader(response
);
152 callback
.Run(PopResponseData(&reader
));
155 void OnKeyPair(const KeyPairCallback
& callback
, dbus::Response
* response
) {
157 callback
.Run("", "");
161 dbus::MessageReader
reader(response
);
162 std::string private_key
= PopResponseData(&reader
);
163 std::string public_key
= PopResponseData(&reader
);
165 if (public_key
.empty() || private_key
.empty()) {
166 callback
.Run("", "");
170 callback
.Run(private_key
, public_key
);
173 dbus::ObjectProxy
* proxy_
;
175 // Note: This should remain the last member so it'll be destroyed and
176 // invalidate its weak pointers before any other members are destroyed.
177 base::WeakPtrFactory
<EasyUnlockClientImpl
> weak_ptr_factory_
;
179 DISALLOW_COPY_AND_ASSIGN(EasyUnlockClientImpl
);
184 EasyUnlockClient::CreateSecureMessageOptions::CreateSecureMessageOptions() {}
186 EasyUnlockClient::CreateSecureMessageOptions::~CreateSecureMessageOptions() {}
188 EasyUnlockClient::UnwrapSecureMessageOptions::UnwrapSecureMessageOptions() {}
190 EasyUnlockClient::UnwrapSecureMessageOptions::~UnwrapSecureMessageOptions() {}
192 EasyUnlockClient::EasyUnlockClient() {
195 EasyUnlockClient::~EasyUnlockClient() {
199 EasyUnlockClient
* EasyUnlockClient::Create() {
200 return new EasyUnlockClientImpl();
203 } // namespace chromeos